package com.codingfly.service.impl;

import com.codingfly.core.exception.BizException;
import com.codingfly.core.jdbc.service.impl.JPAServiceImpl;
import com.codingfly.entity.SysUser;
import com.codingfly.repository.SysUserRepository;
import com.codingfly.service.SysUserService;
import com.github.wenhao.jpa.Specifications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class SysUserServiceImpl extends JPAServiceImpl<SysUserRepository, SysUser, Integer> implements SysUserService {

    @Autowired
    private SysUserRepository sysUserRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedJdbcTemplate;

    @Override
    public SysUser findByUsername(String username) {
        Specification specification = Specifications.and().eq("username", username).build();
        Optional<SysUser> optional = sysUserRepository.findOne(specification);
        return optional.isPresent()? optional.get():null;
    }

    @Override
    public void move(Integer id, String action) {
        if ("up".equals(action) || "down".equals(action)) {
            SysUser user = Optional.ofNullable(findById(id)).orElseThrow(()->new BizException("该用户不存在或者已删除"));
            if (user.getGroupId()!=null && user.getOrderNo()!=null) {
                Map<String, Object> params = new HashMap() {{
                    put("groupId", user.getGroupId());
                    put("orderNo", user.getOrderNo());
                }};
                String sql = null;
                if ("up".equals(action)) {
                    sql = "SELECT id, order_no FROM sys_user WHERE group_id=:groupId AND order_no<:orderNo ORDER BY order_no DESC LIMIT 1";
                } else if ("down".equals(action)) {
                    sql = "SELECT id, order_no FROM sys_user WHERE group_id=:groupId AND order_no>:orderNo ORDER BY order_no ASC LIMIT 1";
                }
                List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sql, params);
                Map<String, Object> map = ObjectUtils.isEmpty(list)? null:list.get(0);
                if (map!=null && map.get("order_no")!=null) {
                    sql = "UPDATE sys_user SET order_no=:orderNo WHERE id=:id";
                    params = new HashMap() {{
                        put("id", user.getId());
                        put("orderNo", Integer.valueOf(map.get("order_no").toString()));
                    }};
                    namedJdbcTemplate.update(sql, params);
                    params = new HashMap() {{
                        put("id", Integer.valueOf(map.get("id").toString()));
                        put("orderNo", user.getOrderNo());
                    }};
                    namedJdbcTemplate.update(sql, params);
                }
            }
        }
    }

}
